package com.marketsim.main;

import com.marketsim.analysis.BigDecimalConverter;
import com.marketsim.analysis.PortfolioUtils;
import com.marketsim.analysis.StatisticCalculatorJ8;
import com.marketsim.common.bean.HistoryPriceBean;
import com.marketsim.common.bean.OrderBean;
import com.marketsim.common.bean.PortfolioValueBean;
import com.marketsim.dao.ExchangesDAO;
import com.marketsim.dao.OrdersDAO;
import com.marketsim.dao.StockTickerDAO;
import com.marketsim.simulator.MarketSimulator;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by Alex on 06.04.2014.
 */
public class Lab3MainApp {

    public static void main(String[] arg) {
        Lab3MainApp mainAppLab3 = new Lab3MainApp();
        mainAppLab3.lab3_MarketSimulator();
    }

    private void lab3_MarketSimulator() {
        MarketSimulator simulator = new MarketSimulator();
        simulator.setStockTickerDAO(StockTickerDAO.getInstance());
        simulator.setExchangesDAO(new ExchangesDAO());
        OrdersDAO ordersDAO = new OrdersDAO();
        File ordersFile = new File("orders.csv");
        List<OrderBean> orders = ordersDAO.getOrdersFromFile(ordersFile);
        List<PortfolioValueBean> portfolioValues = simulator.simulate(orders, new BigDecimal(1000000));

        BigDecimalConverter<PortfolioValueBean> converter = (from) -> from.getAmount();
        List<BigDecimal> portfolioReturns = PortfolioUtils.getReturns(portfolioValues, converter);
        System.out.println("Returns of portfolio " + portfolioReturns);

        System.out.println("Std dev of portfolio returns= " + StatisticCalculatorJ8.getStdDev(portfolioReturns));
        System.out.println("Sharp ratio of portfolio returns= " + StatisticCalculatorJ8.getSharpRatio(portfolioReturns));
        System.out.println("Average daily return of portfolio= " + StatisticCalculatorJ8.getMean(portfolioReturns));

        StockTickerDAO tickerDAO = StockTickerDAO.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("d-M-yyyy", Locale.ENGLISH);
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = dateFormat.parse("10-01-2011");
            endDate = dateFormat.parse("20-12-2011");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<HistoryPriceBean> prices = tickerDAO.getStockInfo("^GSPC", startDate, endDate);
        BigDecimalConverter<HistoryPriceBean> historyConverter = (from) -> from.getAdjClose();
        for (HistoryPriceBean historyPriceBean : prices) {
            System.out.println(historyPriceBean.getAdjClose());
        }
        List<BigDecimal> indexReturns = PortfolioUtils.getReturns(prices, historyConverter);
        System.out.println("SPY RETURNS!!!!");
        System.out.println("Returns of index " + indexReturns);

        System.out.println("Std dev of index returns= " + StatisticCalculatorJ8.getStdDev(indexReturns));
        System.out.println("Sharp ratio of index returns= " + StatisticCalculatorJ8.getSharpRatio(indexReturns));
        System.out.println("Average daily return of index= " + StatisticCalculatorJ8.getMean(indexReturns));

    }
}


